package com.hnevc.nbastarserver.controller;


import com.hnevc.nbastarserver.bean.User;
import com.hnevc.nbastarserver.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    UserDao userDao;
    @ResponseBody
    @PostMapping("/NBAstarApp/user/regist")
    public Map<String, Object> regist(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        User foundUser = userDao.saveAndFlush(user);
        if (foundUser != null) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }


    @ResponseBody
    @PostMapping("/NBAstarApp/user/login")
    public Map<String, Object> login(@RequestParam String name,@RequestParam String password) {
        Map<String, Object> map = new HashMap<>();
        User foundUser = userDao.findOne(Example.of(new User(name,password,null))).orElse(null);
        if (foundUser != null) {
            //验证通过
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", foundUser);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }


}
